#include "lvgl.h"
#include "esp_log.h"
#include "lv_port.h"
#include "st7789_driver.h"
#include "cpu_temp.h"
#include "sd_spi.h"

LV_IMG_DECLARE(temp_img)
LV_IMG_DECLARE(humidity_img)


static lv_timer_t* s_info_refresh_temp_timer;

static lv_obj_t* s_temp_image;
static lv_obj_t* s_humidity_image;

static lv_obj_t* s_temp_label;
static lv_obj_t* s_humidity_label;

static lv_obj_t* s_sd_total_size_label;
static lv_obj_t* s_sd_free_size_label;

static lv_obj_t* s_sd_total_value_label;
static lv_obj_t* s_sd_free_value_label;


static lv_obj_t* s_light_slider;
static lv_obj_t* s_backlight_slider;

static lv_obj_t * s_led_button = NULL;
static lv_obj_t * s_led_label = NULL;

static lv_group_t *group;


void light_slider_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e); // 事件代码，发生的是什么事件
    // uint32_t keyValue = lv_indev_get_key(lv_indev_get_act());
    uint32_t keyValue = 10;
    lv_indev_t * indev = lv_indev_get_act();
    // lv_indev_type_t indev_type = lv_indev_get_type(indev);
    // const uint32_t *key = lv_event_get_user_data(e);
    // if(code == LV_EVENT_CLICKED && indev_type == LV_INDEV_TYPE_KEYPAD) {
    //     ESP_LOGI("LV_EVENT_CLICKED","LV_EVENT_CLICKED ================== LV_INDEV_TYPE_KEYPAD");
    // } 
    switch (code)
    {
        case LV_EVENT_VALUE_CHANGED:
        {       
            lv_obj_t* slider_obj = lv_event_get_target(e);  // 获取控件对象
            int32_t value = lv_slider_get_value(slider_obj);  // 获取slider控件指示的值
            if (value <= 1) {
                lv_slider_set_value(s_light_slider, 1, true);
                set_backlight_pwm_duty(1);
            } else {
                set_backlight_pwm_duty(value);
            }
            break;
        }
        case LV_EVENT_KEY:
            keyValue = lv_indev_get_key(indev);  // 按键事件，则获取按下的按键代码，就是按键值
            if (keyValue == LV_KEY_ENTER){
                // ESP_LOGI("UI_HOME","keyValue ================== 10");
                // lv_group_t * g = i->group; // 获取输入设备关联的组
                // if(g == NULL) {}; // 如果没有关联组
                // indev_obj_act = lv_group_get_focused(g); // 获取当前焦点对象
                // if(indev_obj_act == NULL) return; // 如果没有焦点对象，则直接返回
                if (lv_group_get_editing(group)) {   // 更优雅的方式，是获取控件对象（lv_group_get_editing(g)），判断控件对象当前是否是editing状态
                    set_editing(false);
                    lv_group_set_editing(group, false);
                } else {
                    set_editing(true);
                    lv_group_set_editing(group, true);
                }
            }
            
        default:
            break;
    }
}

void light_slider_demo_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e); // 事件代码，发生的是什么事件
    uint32_t keyValue = 10;
    lv_indev_t * indev = lv_indev_get_act();
   
    switch (code)
    {
        case LV_EVENT_VALUE_CHANGED:
        {       
            break;
        }
        case LV_EVENT_KEY:
            keyValue = lv_indev_get_key(indev);  // 按键事件，则获取按下的按键代码，就是按键值
            if (keyValue == LV_KEY_ENTER){
                if (lv_group_get_editing(group)) {   // 更优雅的方式，是获取控件对象（lv_group_get_editing(g)），判断控件对象当前是否是editing状态
                    set_editing(false);
                    lv_group_set_editing(group, false);
                } else {
                    set_editing(true);
                    lv_group_set_editing(group, true);
                }
            }
            
        default:
            break;
    }
}

void info_refresh_timer_cb(struct _lv_timer_t *t)
{
    float temp = getCpuTemp();
    if(temp)
    {
        char disp_buf[32];
        snprintf(disp_buf,sizeof(disp_buf),"%.1f", temp);
        lv_label_set_text(s_temp_label, disp_buf);
    }
    uint32_t total_kb = 0;
    uint32_t free_kb = 0;
    if (exf_getfree(&total_kb, &free_kb) == 0) {
        char sizeFree[64];
        snprintf(sizeFree, sizeof(sizeFree), "SD: %lu/%luMB", (uint32_t)(free_kb / 1024), (uint32_t)(total_kb / 1024));
        // snprintf(disp_buf,sizeof(disp_buf),"%.1f", temp);
        lv_label_set_text(s_sd_total_value_label, sizeFree);
    } else {
        lv_label_set_text(s_sd_total_value_label, "SD: ?/?MB");
    }
}

void ui_home_create(void)
{
    group = get_group();
    //创建调光用的进度条
    lv_obj_set_style_bg_color(lv_scr_act(), lv_color_black(),0);

    s_light_slider = lv_slider_create(lv_scr_act());
    lv_obj_set_pos(s_light_slider,170, 40);
    lv_obj_set_size(s_light_slider,120, 15);
    lv_slider_set_range(s_light_slider,0,100);
    lv_slider_set_value(s_light_slider, 30, true);
    lv_obj_add_event_cb(s_light_slider,light_slider_event_cb,LV_EVENT_ALL,NULL);//s_backlight_slider

    s_backlight_slider = lv_slider_create(lv_scr_act());
    lv_obj_set_pos(s_backlight_slider,170, 80);
    lv_obj_set_size(s_backlight_slider,120, 15);
    lv_slider_set_range(s_backlight_slider,0,100);
    lv_obj_add_event_cb(s_backlight_slider,light_slider_demo_event_cb, LV_EVENT_ALL,NULL);//s_backlight_slider


    // static lv_obj_t* s_sd_total_size_label;
    // static lv_obj_t* s_sd_free_size_label;
    // static lv_obj_t* s_sd_total_value_label;
    // static lv_obj_t* s_sd_free_value_label;
    //创建sd卡空间大小信息
    s_sd_total_value_label = lv_label_create(lv_scr_act());
    lv_obj_set_pos(s_sd_total_value_label,170,110);
    lv_obj_set_style_text_font(s_sd_total_value_label, &lv_font_montserrat_14, 0);
    lv_label_set_text(s_sd_total_value_label, "SD: ?/?MB");

    //创建温度图片
    s_temp_image = lv_img_create(lv_scr_act());
    lv_img_set_src(s_temp_image,&temp_img);
    lv_obj_set_pos(s_temp_image,0,20);

    //创建湿度图片
    s_humidity_image = lv_img_create(lv_scr_act());
    lv_img_set_src(s_humidity_image,&humidity_img);
    lv_obj_set_pos(s_humidity_image,0,95);

    //创建温度label
    s_temp_label = lv_label_create(lv_scr_act());
    lv_obj_set_pos(s_temp_label,70,25);
    lv_obj_set_style_text_font(s_temp_label,&lv_font_montserrat_38,0);

    //创建湿度label
    s_humidity_label = lv_label_create(lv_scr_act());
    lv_obj_set_pos(s_humidity_label,70, 105);
    lv_obj_set_style_text_font(s_humidity_label,&lv_font_montserrat_38,0);

    char disp_buf[32];
    snprintf(disp_buf,sizeof(disp_buf),"%.1f",(float)26.8);
    lv_label_set_text(s_temp_label,disp_buf);

    snprintf(disp_buf,sizeof(disp_buf),"%d%%", 82);
    lv_label_set_text(s_humidity_label,disp_buf);


    //创建定时器
    s_info_refresh_temp_timer = lv_timer_create(info_refresh_timer_cb,2000,NULL);

    /* 添加组 */
    // lv_group_add_obj(group, s_temp_image);
    // lv_group_add_obj(group, s_humidity_image);
    lv_group_add_obj(group, s_light_slider);
    lv_group_add_obj(group, s_backlight_slider);
}
